package com.fyy.media.api;

import com.fyy.base.model.PageParams;
import com.fyy.base.model.PageResult;
import com.fyy.media.model.dto.QueryMediaParamsDto;
import com.fyy.media.model.dto.UploadFileParamsDTO;
import com.fyy.media.model.dto.UploadFileResultDTO;
import com.fyy.media.model.po.MediaFiles;
import com.fyy.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @author fyy
 * @version 1.0
 * @description 媒资文件管理接口
 * @date 2024/8/1 22:55
 */
@Api(value = "媒资文件管理接口", tags = "媒资文件管理接口")
@RestController
public class MediaFilesController {


    @Autowired
    private MediaFileService mediaFileService;


    @ApiOperation("媒资列表查询接口")
    @PostMapping("/files")
    public PageResult<MediaFiles> list(PageParams pageParams, @RequestBody QueryMediaParamsDto queryMediaParamsDto) {
        Long companyId = 1232141425L;
        return mediaFileService.queryMediaFiels(companyId, pageParams, queryMediaParamsDto);

    }

    @ApiOperation("上传文件")
    @RequestMapping(value = "/upload/coursefile", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @ResponseBody
    public UploadFileResultDTO upload(
            @RequestPart("filedata") MultipartFile fileData,
            @RequestParam(value = "folder", required = false) String folder,
            @RequestParam(value = "objectName", required = false) String objectName) throws IOException {
        Long companyId = 1232141425L;
        UploadFileParamsDTO uploadFileParamsDTO = new UploadFileParamsDTO();
        uploadFileParamsDTO.setFileSize(fileData.getSize());
        // 图片对应的字典
        uploadFileParamsDTO.setFileType("001001");
        uploadFileParamsDTO.setFilename(fileData.getOriginalFilename());
        // todo 为什么需要本地暂存
        File tempFile = File.createTempFile("minio", "temp");
        fileData.transferTo(tempFile);
        String absolutePath = tempFile.getAbsolutePath();
        UploadFileResultDTO uploadFileResultDTO = mediaFileService.uploadFile(companyId, uploadFileParamsDTO, absolutePath);
        return uploadFileResultDTO;
    }

}
